//
//  SettingTableViewController.m
//  ALS3.0
//
//  Created by guomianxiang on 17/3/13.
//  Copyright © 2017年 guomianxiang. All rights reserved.
//

#import "SettingTableViewController.h"
#import "TouchIDManager.h"
#import "ALSHeader.h"
#import "LoginWindow.h"
#import "LGtools.h"
@interface SettingTableViewController ()

@property (weak, nonatomic) IBOutlet UISwitch *touchIDLogin;
@property (weak, nonatomic) IBOutlet UISwitch *autoLogout;
@property (weak, nonatomic) IBOutlet UILabel *verLab;

@end

@implementation SettingTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.touchIDLogin setOn:NSUDIsTouchIDLogin];
    
    [self.autoLogout setOn:NSUDIsAutoLogout];
    
    _verLab.text = [@"Ver" stringByAppendingString: [[[NSBundle mainBundle]infoDictionary] valueForKey:@"CFBundleShortVersionString"]];

}
// 指纹登陆
- (IBAction)touchIDLogin:(UISwitch *)sender {
    [NSUD setBool:sender.on forKey:ALSTouchIDLogin];

}
// 后台自动退出
- (IBAction)autoLogout:(UISwitch *)sender {
    [NSUD setBool:sender.on forKey:ALSAutoLogout];
}
// 退出登陆
- (IBAction)logout:(UIButton *)sender {

    UIApplication *application = [UIApplication sharedApplication];
    UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    
    application.keyWindow.rootViewController = [storyBoard instantiateInitialViewController];
    
    [NSUD setBool:NO forKey:ALSAdministrators];
    [NSUD setBool:NO forKey:ALSStartTouchID];
//    [NSUD setBool:NO forKey:ALSLoginState];
    
    [[LoginWindow shareLoginWindw]show];
    [MobClick event:UMEventLogout];

}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 4;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    NSInteger row = 0;
    
    switch (section) {
        case 0:
            row = 1;
            break;
        case 1:
            row = 3;
            break;
        case 2:
            row = 2;
            break;
        case 3:
            row = 1;
            break;
        default:
            break;
    }

    return row;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    if (indexPath.section == 0) {
        
        UILabel * testlable = [[UILabel alloc]init];
        
        NSString * tstring =@"杭州阿里山电子有限公司，创办于2004年。总部位于美丽的西子湖畔 京杭大运河最南端杭州市拱墅区，阿里山公司通过致力于智能化电气工程技术应用服务而创造美好生活作为毕生的追求目标。目前公司业务范围包括智能门禁系统、智能家居系统、以及相关安防产业系统。";
        
        UIFont * tfont = [UIFont systemFontOfSize:10];
        
        testlable.font = tfont;
        
        testlable.lineBreakMode =NSLineBreakByCharWrapping;
        
        
        [testlable setBackgroundColor:[UIColor redColor]];
        testlable.text = tstring;
//        [self.view addSubview:testlable];
        
        //高度估计文本大概要显示几行，宽度根据需求自己定义。 MAXFLOAT 可以算出具体要多高
        
        //    CGSize size =CGSizeMake(300,60);
        
        // label可设置的最大高度和宽度
        CGSize size = CGSizeMake(kScreen_W - 20, MAXFLOAT);
        
        //    获取当前文本的属性
        
        NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:tfont,NSFontAttributeName,nil];
        
        //ios7方法，获取文本需要的size，限制宽度
        
        CGSize  actualsize =[tstring boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin  attributes:tdic context:nil].size;
        
        // ios7之前使用方法获取文本需要的size，7.0已弃用下面的方法。此方法要求font，与breakmode与之前设置的完全一致
        //    CGSize actualsize = [tstring sizeWithFont:font constrainedToSize:size lineBreakMode:NSLineBreakByCharWrapping];
        //   更新UILabel的frame
        
        
//        testlable.frame =CGRectMake(10,20, actualsize.width, actualsize.height);

//        NSLog(@"%f",actualsize.height);

        return (int)actualsize.height + 68 + 14 + 36 + 20;
    }
    
    if (indexPath.section == 2 && indexPath.row == 0) {
        // touchID 可以使用返回 44
        if ([TouchIDManager sharedTouchIDManager].canUseTouchID) {
            return 44;
        }
        return 0;
    }

    return 44;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    }
    else {
        return 20;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 1;
}

/*
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:<#@"reuseIdentifier"#> forIndexPath:indexPath];
    
    // Configure the cell...
    
    return cell;
}
*/

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
